<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="分类名称" prop="name">
				<el-input v-model="form.name" :maxlength="255" show-word-limit></el-input>
			</el-form-item>
			<el-form-item label="排序值" prop="sort">
				<el-input-number v-model="form.sort" :min="0" :max="255" controls-position="right" style="width: 200px;"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio v-model="form.status" :label="1">启用</el-radio>
				<el-radio v-model="form.status" :label="2">禁用</el-radio>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="optSubmit('ruleForm')" :loading="loading">确 定</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增品牌分类',
				edit: '编辑品牌分类',
				show: '查看'
			},
			form: {
				id: 0,
				name: '',
				sort: 0,
				status: 1
			},
			rules: {
				name: [
					{required: true, message: '请输入分类名称', trigger: 'blur'}
				],
				sort: [
					{required: true, message: '请输入排序值', trigger: 'blur'}
				],
				status: [
					{required: true, message: '请选择状态', trigger: 'change'}
				]
			},
			loading: false,
			visible: false
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单提交方法
		optSubmit(formName) {
			this.$refs[formName].validate(async (valid) => {
				if (valid) {
					this.loading = true
					let res;
					if (this.mode == 'add') {
						res = await this.$API.brandCate.add.post(this.form)
					} else {
						res = await this.$API.brandCate.edit.post(this.form)
					}
					this.loading = false

					if (res.code == 0) {
						this.$message.success(res.msg)
						this.$emit('success')
					} else {
						this.$message.error(res.msg)
					}
				} else {
					return false;
				}
			})
		},
		// 表单注入数据
		setData(item) {
			this.form.id = item.id
			this.form.name = item.name
			this.form.sort = item.sort
			this.form.status = item.status
		}
	}
}
</script>

<style scoped>
</style>